﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;


namespace Planetwork
{
    public partial class Register : System.Web.UI.Page
    {
        static string conStr = ConfigurationManager.AppSettings["conStr"];
        SqlConnection mycon = new SqlConnection(conStr);
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UserName.Attributes.Add("placeholder", "请输入用户名");
            this.Password.Attributes.Add("placeholder", "密码");
            this.Email.Attributes.Add("placeholder", "Emial");
            this.Name.Attributes.Add("placeholder", "你的名字");
        }

        /// <summary>
        /// 注册事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RegisterBtn_Click(object sender, EventArgs e)
        {
            string sql = "select *from Account";
            SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
            DataSet myds = new DataSet();

            mycon.Open();
            mydat.Fill(myds);
            mycon.Close();

            DataTable table = myds.Tables[0];

            //判断用户名和邮箱是否已存在
            var userNmaeOnly = table.AsEnumerable().Where(p => p.Field<string>("UserName").ToLower() == this.UserName.Text.Trim().ToLower()).FirstOrDefault();
            if (userNmaeOnly != null)
            {
                Response.Write("<script>alert('用户名已经存在');</script>");
                return;
            }
            var emailOnly = table.AsEnumerable().Where(p => p.Field<string>("Email").ToLower() == this.Email.Text.Trim().ToLower()).FirstOrDefault();
            if(emailOnly != null)
            {
                Response.Write("<script>alert('Email已经存在');</script>");
                return;
            }

            //将注册用户写入数据库
            sql = "insert into Account (UserName, UserPassword, Email, Name) values(@userName, @password, @email, @name)";
            SqlCommand mycmd = new SqlCommand(sql, mycon);
            mycmd.Parameters.AddWithValue("@userName", this.UserName.Text.Trim().ToLower());
            mycmd.Parameters.AddWithValue("@password", this.Password.Text.Trim());
            mycmd.Parameters.AddWithValue("@email", this.Email.Text.Trim().ToLower());
            mycmd.Parameters.AddWithValue("@name", this.Name.Text.Trim());

            mycon.Open();
            int i = mycmd.ExecuteNonQuery();
            mycon.Close();

            
            if(i > 0)
            {
                Session["name"] = this.UserName.Text.Trim();
                FormsAuthentication.SetAuthCookie(this.UserName.Text.Trim(), false);
                Response.Write("<script>alert('注册成功');window.location.href='MyInfo.aspx'</script>");
            }
            else
                Response.Write("<script>alert('注册失败');</script>");
        }
    }
}